let http = require('http');
let querystring = require('querystring');
let crypto = require('crypto');
let uuid = require('uuid');

let sessionId = 'wash';

let session = {

}
http.createServer(function (req, res) {
  if (req.url==='/to_wash'){
    let cookies = querystring.parse(req.headers.cookie,'; ')||{};
    let username = cookies[sessionId];
    if (username&& session[username]){
      session[username].mny -=10;
      res.setHeader('Content-Type',`text/html;charset=utf8`);
      res.end(`当前您的额度是${session[username].mny}`)

    }else {
      console.log('hello')
      let cardId = uuid.v4();
      //第一次来店里
      res.setHeader('Set-Cookie',`${sessionId}=${cardId}; httpOnly=true`);
      res.setHeader('Content-Type',`text/html;charset=utf8`);
      session[cardId]={
        mny:100
      };
      res.end(`当前您的额度是${session[cardId].mny}`)
    }
  }
  console.log('错误')
}).listen(3000);
